'use strict';

/**
 * 用指定的`config`、`error code`和`response`修改一个error。
 *
 * @param {Error} error 要被修改的error.
 * @param {Object} config `config`配置.
 * @param {string} [code] `error code`错误代码 (比如：'ECONNABORTED').
 * @param {Object} [request] `request`请求.
 * @param {Object} [response] `response`响应.
 * @returns {Error} The error. 增强后的错误
 */
module.exports = function enhanceError(error, config, code, request, response) {
  error.config = config;
  if (code) {
    error.code = code;
  }

  error.request = request;
  error.response = response;
  error.isAxiosError = true;

  error.toJSON = function toJSON() {
    return {
      // Standard
      message: this.message,
      name: this.name,
      // Microsoft
      description: this.description,
      number: this.number,
      // Mozilla
      fileName: this.fileName,
      lineNumber: this.lineNumber,
      columnNumber: this.columnNumber,
      stack: this.stack,
      // Axios
      config: this.config,
      code: this.code,
      status:
        this.response && this.response.status ? this.response.status : null,
    };
  };
  return error;
};
